package edu.zhku.ttms.view.panel.content;

import edu.zhku.ttms.service.UserService;
import edu.zhku.ttms.service.impl.UserServiceImpl;
import edu.zhku.ttms.utils.CheckUtil;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.font.LeftLabelFont;
import edu.zhku.ttms.view.label.HeaderLabel;

import javax.swing.*;
import java.awt.*;

/**
 * @author Feng
 * @since 2021/4/23 22:02
 */
public class LoginPanel extends ViewPanel {

    static UserService userService = new UserServiceImpl();
    
    public LoginPanel() {
        
        super(false);

        this.add(new HeaderLabel());

        Font font = new LeftLabelFont();

        JLabel loginNameLabel = new JLabel("用户名");
        loginNameLabel.setFont(font);
        loginNameLabel.setBounds(260, 215, 132, 30);
        this.add(loginNameLabel);

        JTextField loginNameInput = new JTextField();
        loginNameInput.setBounds(392, 215, 308, 30);
        this.add(loginNameInput);

        JLabel passwordLabel = new JLabel("密码");
        passwordLabel.setFont(font);
        passwordLabel.setBounds(260, 270, 132, 30);
        this.add(passwordLabel);

        JPasswordField passwordInput = new JPasswordField();
        passwordInput.setBounds(392, 270, 308, 30);
        this.add(passwordInput);

        JLabel tip = new JLabel();
        tip.setForeground(Color.RED);
        tip.setFont(font);
        tip.setBounds(392, 325, 440, 30);
        this.add(tip);

        JButton toRegisterButton = new JButton("前往注册");
        toRegisterButton.addActionListener(e -> ViewController.changeView(new RegisterPanel()));
        toRegisterButton.setBounds(260, 380, 140, 48);
        this.add(toRegisterButton);

        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(e -> {
            String loginName = loginNameInput.getText();
            String password = passwordInput.getText();
            String login = userService.login(loginName, password);
            if (CheckUtil.isSuccess(login)) {
                ViewController.changeView(new SuccessPanel());
            } else {
                tip.setText(login);
            }
        });
        loginButton.setBounds(546, 380, 148, 48);
        this.add(loginButton);
    }
}
